package com.wy.component.dataConvert;

import java.util.List;
import java.util.Map;

import org.json.JSONArray;

public class ConvertToJson {
	/*
	 * 将一个list转换成json /
	 */
	public static String List2Json(List<?> list, boolean res) {
		try {
			if (list == null || list.size() == 0) {
				return "[]";
			}
			JSONArray jsonArray = new JSONArray(list);
			String jsonStr = "{\"" + list.get(0).getClass().getSimpleName()
					+ "\":" + jsonArray + ",\"res\":\"" + res
					+ "\"}".toString();
			return jsonStr;
		} catch (Exception e) {
			return "[]";
		}
	}

	public static String List2JsonMeeting(List<?> list, String currentDate,
			String currentWeek, String maxWeek) {
		try {
			if (list == null || list.size() == 0) {
				return "{\"Meetings\":[],\"currentDate\":\"" + currentDate
						+ "\",\"currentWeek\":\"" + currentWeek
						+ "\",\"maxWeek\":\"" + maxWeek + "\"}";
			}
			JSONArray jsonArray = new JSONArray(list);
			String jsonStr = "{\"" + list.get(0).getClass().getSimpleName()
					+ "\":" + jsonArray + ",\"currentDate\":\"" + currentDate
					+ "\",\"currentWeek\":\"" + currentWeek
					+ "\",\"maxWeek\":\"" + maxWeek + "\"}".toString();
			return jsonStr;
		} catch (Exception e) {
			return "[]";
		}
	}

	public static String List2Json(List<?> list, boolean res, String className) {
		try {
			if (list == null || list.size() == 0) {
				return "{\"" + className + "\":" + "[]" + ",\"res\":\"" + res
						+ "\"}".toString();
			}
			JSONArray jsonArray = new JSONArray(list);
			String jsonStr = "{\"" + className + "\":" + jsonArray
					+ ",\"res\":\"" + res + "\"}".toString();
			return jsonStr;
		} catch (Exception e) {
			return "[]";
		}
	}

	/*
	 * 将map转换成json /
	 */
	public static String MapJson(Map<String, Object> map, boolean res,
			String className) {
		try {
			if (map == null || map.size() == 0) {
				return "{\"" + className + "\":" + "[]" + ",\"res\":\"" + res
						+ "\"}".toString();
			}
			JSONArray jsonArray = new JSONArray(map);
			String jsonStr = "{\"" + className + "\":" + jsonArray
					+ ",\"res\":\"" + res + "\"}".toString();
			return jsonStr;
		} catch (Exception e) {
			return "[]";
		}
	}

	/*
	 * 将特殊map转换成给二维码用的json /
	 */
	public static String MapJsonToRWM(String id, String title, String type) {
		try {
			return "{\"id\":\"" + id + "\",\"title\":\"" + title
					+ "\",\"type\":\"" + type + "\"}";
		} catch (Exception e) {
			return "[]";
		}

	}

	/*
	 * 将一个对象转换成json /
	 */
	public static String Object2Json(Object obj, boolean res) {

		try {
			if (obj == null || obj.equals("")) {
				return "[]";
			}
			JSONArray jsonArray = new JSONArray(obj);
			String jsonStr = "{\"" + obj.getClass().getSimpleName() + "\":"
					+ jsonArray + ",\"res\":\"" + res + "\"}".toString();
			return jsonStr;
		} catch (Exception e) {
			e.printStackTrace();
			return "[]";
		}

	}

	/*
	 * 将一个对象转换成json / 传名字
	 */
	public static String Object2Json(Object obj, boolean res, String name) {

		try {
			if (obj == null || obj.equals("")) {
				return "{\"" + name + "\":" + "[]" + ",\"res\":\"" + res
						+ "\"}".toString();
			}
			JSONArray jsonArray = new JSONArray(obj);
			String jsonStr = "{\"" + name + "\":" + jsonArray + ",\"res\":\""
					+ res + "\"}".toString();
			return jsonStr;
		} catch (Exception e) {
			e.printStackTrace();
			return "{\"" + name + "\":" + "[]" + ",\"res\":\"" + res
					+ "\"}".toString();
		}

	}

	/*
	 * 将多个list转换成json /
	 */
	public static String Lisys2Json(List<?> list1, List<?> list2, boolean res) {
		try {
			if (list1.size() == 0 || list1 == null || list2.size() == 0
					|| list2 == null) {
				return "[]";
			}
			JSONArray jsonArray = new JSONArray(list1);
			JSONArray jsonArrays = new JSONArray(list2);

			String jsonStr = "{\"" + list1.get(0).getClass().getSimpleName()
					+ "\":" + jsonArray + ",\""
					+ list2.get(0).getClass().getSimpleName() + "\":"
					+ jsonArrays + ",\"res\":\"" + res + "\"}".toString();
			return jsonStr;
		} catch (Exception e) {
			return "[]";
		}

	}

	// 将多个list转换成json
	public static String Lisys2Json(List<?> list1, List<?> list2,
			String className1, String className2, boolean res) {
		try {
			if (list1.size() == 0 || list1 == null || list2.size() == 0
					|| list2 == null) {
				return "{\"" + className1 + "\":" + "[]" + ",\"" + className2
						+ "\":" + "[]" + ",\"res\":\"" + res + "\"}".toString();
			}
			JSONArray jsonArray = new JSONArray(list1);
			JSONArray jsonArrays = new JSONArray(list2);

			String jsonStr = "{\"" + className1 + "\":" + jsonArray + ",\""
					+ className2 + "\":" + jsonArrays + ",\"res\":\"" + res
					+ "\"}".toString();
			return jsonStr;
		} catch (Exception e) {
			e.printStackTrace();
			return "{\"" + className1 + "\":" + "[]" + ",\"" + className2
					+ "\":" + "[]" + ",\"res\":\"" + res + "\"}".toString();
		}

	}
}
